package com.iflytek.autofly.setting.biz.secret;

import android.text.TextUtils;

import com.iflytek.autofly.utils.FlyAssert;
import com.iflytek.autofly.utils.FlyLog;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

/**
 * <b>Description: </b>高级设置控制类<br/>
 * <b>Change History:</b><br/>
 * <table border="1">
 * <tr>
 * <td><b>user</b></td>
 * <td><b>date</b></td>
 * <td><b>time</b></td>
 * <td><b>reason</b></td>
 * </tr>
 * <tr>
 * <td>JunLuan@iflytek.com</td>
 * <td>2015年5月29日</td>
 * <td>上午10:04:57</td>
 * <td>创建此文件</td>
 * </tr>
 * </table>
 *
 * @author JunLuan@iflytek.com
 */
public class LogSetting {
    private static final String TAG = "LogSetting";

    int mLevel = -1;
    int mOutput = -1;

    private LogSetting() {
        mLevel = FlyLog.Config.level;
        mOutput = FlyLog.Config.output;
        FlyLog.d(TAG, "level:" + mLevel + " ouput:" + mOutput);
    }

    public static LogSetting getInstance() {
        return LogSettingInstance.INSTANCE;
    }

    public int getNetworkTimeout() {
        // TODO Auto-generated method stub
        String a = System.getProperty("xx");
        if (a != null) {
            return Integer.parseInt(a);

        }
        return 5000;
    }

    public int setNetworkTimeout(int value) {
        // TODO Auto-generated method stub
        System.setProperty("xx", String.valueOf(value));
        return 0;
    }

    public void enableLevel(int level) {
        mLevel |= level;
        setLogLevel();
    }

    public void disableLevel(int level) {
        mLevel = (mLevel | level) ^ level;
        setLogLevel();
    }

    public boolean isLevelEnabled(int level) {

        return (mLevel & level) != 0;
    }

    public void enableOutput(int output) {
        mOutput |= output;
        setLogOutput();
    }

    public void disableOutput(int output) {
        mOutput = (mOutput | output) ^ output;
        setLogOutput();
    }

    public boolean isOutputEnabled(int output) {

        return (mOutput & output) != 0;
    }

    public String getLogSeparator() {
        return FlyLog.Config.separator;
    }

    public void setLogSeparator(String s) {
        setProperty("log.ifly.separator", s);
    }

    public void enableAssert() {
        setProperty("log.ifly.assert", String.valueOf(true));
    }

    public void disableAssert() {
        setProperty("log.ifly.assert", String.valueOf(false));
    }

    public boolean isAssertOn() {
        return FlyAssert.Config.mode;
    }

    private void setLogLevel() {
        String value = String.valueOf(mLevel);
        if (!TextUtils.isEmpty(value)) {
            setProperty("log.ifly.level", value);
        }
    }

    private void setLogOutput() {
        String value = String.valueOf(mOutput);
        if (!TextUtils.isEmpty(value)) {
            setProperty("log.ifly.ouput", value);
        }
    }

    private void setProperty(String key, String value) {
        String filename = "/data/data/.flylog.properties";

        File file = new File(filename);
        FileInputStream fileInStream = null;
        OutputStream fileOutStream = null;
        Properties prop = new Properties();
        Process process = null;
        DataOutputStream processOutStream = null;

        try {
            fileInStream = new FileInputStream(file);
            prop.load(fileInStream);

            prop.setProperty(key, value);

            fileOutStream = new FileOutputStream(file);
            prop.store(fileOutStream, null);

        } catch (FileNotFoundException ignore) {
        } catch (IOException ignore) {
        } finally {
            try {
                if (fileInStream != null) {
                    fileInStream.close();
                }
            } catch (IOException ignore) {
            }
            try {
                if (fileOutStream != null) {
                    fileOutStream.close();
                }
            } catch (IOException ignore) {
            }
            try {
                if (processOutStream != null) {
                    processOutStream.close();
                }
            } catch (IOException ignore) {
            }
            try {
                if (processOutStream != null) {
                    processOutStream.close();
                }
            } catch (Exception ignore) {
            }
            if (process != null) {
                process.destroy();
            }
        }

        // Class<?> systemPropertiesClass;
        // Method setPropertyMethod;
        // try {
        // systemPropertiesClass = Class.forName("android.os.SystemProperties");
        // if (systemPropertiesClass != null) {
        // setPropertyMethod = systemPropertiesClass.getDeclaredMethod("set",
        // String.class, String.class);
        // if (setPropertyMethod != null) {
        // if (!TextUtils.isEmpty(s)) {
        // setPropertyMethod.invoke(systemPropertiesClass, "log.ifly.separator",
        // s);
        // }
        // }
        // }
        // } catch (Exception e) {
        // e.printStackTrace();
        // }
    }

    /**
     * <b>Description: </b>用于单体<br/>
     * <b>Change History:</b><br/>
     * <table border="1">
     * <tr>
     * <td><b>user</b></td>
     * <td><b>date</b></td>
     * <td><b>time</b></td>
     * <td><b>reason</b></td>
     * </tr>
     * <tr>
     * <td>JunLuan@iflytek.com</td>
     * <td>2015年5月29日</td>
     * <td>上午10:40:29</td>
     * <td>创建此文件</td>
     * </tr>
     * </table>
     *
     * @author JunLuan@iflytek.com
     */
    private static class LogSettingInstance {
        private static final LogSetting INSTANCE = new LogSetting();
    }
}
